let liste:int list ref = ref [];;
let nbInstr=ref 0;;
let nbLabel=ref 3;;
let labels:(string, int) Hashtbl.t = Hashtbl.create 31;;
let labelsToInstr:(int, int) Hashtbl.t = Hashtbl.create 31;;


let rec print = function
  |[]->()
  |t::q->
    if t>1 then begin print_newline();print q end
    else begin print_string " ";print_int t;print q end;;


let rec itlp entier = function
  | 0 -> []
  | n -> (entier mod 2)::(itlp (entier/2) (n-1));;

let rec notBB = function
  | [] -> []
  | t::q -> (1-t)::(notBB q);;

let rec addI = function
  | ([],n) -> []
  | (t::q,r) -> ((t+r) mod 2)::(addI (q,(t+r)/2));;

let itl entier n = 
  if entier>=0 then
    itlp entier n
  else begin
    let x = itlp (-entier) n in
    let nx = notBB x in
    addI (nx,1)
  end;;

let rec purify = function
  |[]->[]
  |0::q -> 0::(purify q)
  |1::q -> 1::(purify q)
  |2::q -> 2::(purify q)
  |id::instr::taille::q->
    try
      let valSaut = (Hashtbl.find labelsToInstr id)-instr in
      (List.rev (itl valSaut taille))@(purify q)
    with _-> failwith("erreur purify") 
  |_->failwith("");;

